<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
);

//流失列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'isdel'=>1);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$list=pdo_getslice('jy_scrm_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=array('username'=>$item['name'],'avatar'=>$item['avatar'],'nickname'=>$item['name']);
			$item['workerid']=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$item['follow_user_userid']?:$item['follow_user_oper_userid'],'uniacid'=>$_SESSION['uniacid']),'id');
			$item['tags']=json_decode($item['tags'],true);
			$item['tagstitle']='';
			foreach($item['tags'] as $tv){
				if($item['tagstitle']){
					$item['tagstitle'] .=';'.$tv['tag_name'];
				}else{
					$item['tagstitle']=$tv['tag_name'];
				}
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：当你被微信联系人从好友列表中删除，企业微信营销工具会自动发一条消息通知你。你也可以在企业微信营销工具后台查看历史被删除记录，知道是谁在什么时间把你删除。配合带参数的对外名片功能，也可以知道这个人是从什么渠道来的。
		','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'member','title'=>'客户信息','type'=>'member'),
			array('name'=>'tagstitle','title'=>'标签','type'=>'string'),
			array('name'=>'comefrom','title'=>'来源','type'=>'radio','options'=>'user_froms'),
			array('name'=>'workerid','title'=>'归属成员','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'del_time','title'=>'删除时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>'客户名称','type'=>'string','width'=>'15'),
			array('type'=>'choose','width'=>15,'title'=>'部门成员','name'=>'workerid','action'=>'jy_scrm/worker.get_worker/'),
		),	
	);
	$return['user_froms']=$this->user_froms;
	$return['workers']=$this->getall('worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_member',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}